package com.david.appletree;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class StepActivity extends Activity {
   private TextView mStepTV;
   private SensorManager mSensorManager;
   private MySensorEventListener mListener;
   private int mStepDetector = 0;  // 自应用运行以来STEP_DETECTOR检测到的步数
   private int mStepCounter = 0;   // 自系统开机以来STEP_COUNTER检测到的步数

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(com.david.appletree.R.layout.activity_step);

      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
         Log.d("TAG", "PERMISSION 'ACTIVITY_RECOGNITION' NOT GRANTED");

         ActivityCompat.requestPermissions(this,
                 new String[]{Manifest.permission.ACTIVITY_RECOGNITION},
                 0);
      } else
      {
         Log.d("TAG", "PERMISSION 'ACTIVITY_RECOGNITION' GRANTED");
      }

      mStepTV = (TextView)findViewById(com.david.appletree.R.id.tv_step);
      mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      mListener = new MySensorEventListener();
   }

   @Override
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      Log.e("permission", "requestCode:" +requestCode);
   }

   @Override
   protected void onResume() {
      super.onResume();
      mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR),
              SensorManager.SENSOR_DELAY_NORMAL);
      mSensorManager.registerListener(mListener, mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER),
              SensorManager.SENSOR_DELAY_NORMAL);
   }

   @Override
   protected void onPause() {
      super.onPause();
      mSensorManager.unregisterListener(mListener);
   }

   class MySensorEventListener implements SensorEventListener {
      @Override
      public void onSensorChanged(SensorEvent event) {
         System.out.println("@@@:"+event.sensor.getType()+"--"+Sensor.TYPE_STEP_DETECTOR+"--"+Sensor.TYPE_STEP_COUNTER);
         if (event.sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
            if (event.values[0] == 1.0f) {
               mStepDetector++;
            }
         } else if (event.sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
            mStepCounter = (int) event.values[0];
         }

         String desc = String.format("设备检测到您当前走了%d步，自开机以来总数为%d步", mStepDetector, mStepCounter);
         mStepTV.setText(desc);
      }

      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
      }
   }
}
